//
//  ChooseOrderVC.m
//  quickBatchManager
//
//  Created by jack on 15/9/29.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "ChooseOrderVC.h"
#import "ProductInfoModel.h"
@interface ChooseOrderVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@end

@implementation ChooseOrderVC
{
    NSArray *dataList;
}
@synthesize searchTextField,mTableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [searchTextField becomeFirstResponder];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.backgroundColor = [UIColor clearColor];
    searchTextField.delegate = self;
    
}


-(void)initData
{
    dataList =[ProductModel searchWithWhere:nil orderBy:nil offset:0 count:0];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CMainCell = @"defaultCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier: CMainCell];
    }
  
    ProductModel *data = [dataList objectAtIndex:indexPath.row];
    cell.textLabel.text = data.modelNo;
    [cell setBackgroundColor:[UIColor clearColor]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if(_delegate!=nil && [_delegate respondsToSelector:@selector(ChooseProduct:)])
    {
         ProductModel *data = [dataList objectAtIndex:indexPath.row];
        [_delegate ChooseProduct:data];
    }
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    
     if (searchText!=nil && searchText.length>0)
     {
         dataList=[AppService searchProductByKeyWord:searchText];
        [mTableView reloadData];
     }
     else
     {
         dataList =[ProductModel searchWithWhere:nil orderBy:nil offset:0 count:0]
         ;
        [mTableView reloadData];
     }

}

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchBar:searchBar textDidChange:searchBar.text];
    [searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [self searchBar:searchBar textDidChange:searchBar.text];
    [searchBar resignFirstResponder];
}

@end
